/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Comment;
import com.simon.mengkou.data.bean.base.GroupBuy;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.ShareOrderDetailActivity;
import com.simon.mengkou.ui.view.AvatarSimpleDraweeView;
import com.simon.mengkou.ui.view.MyListView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/28.
 * desc    : 晒单评论适配器
 */
public class CommentAdapter extends AbsAdapter<Comment> {
    private ShareOrderDetailActivity mActivity;

    public CommentAdapter(Context context, ShareOrderDetailActivity activity) {
        super(context);
        this.mActivity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_share_order_comment_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Comment data = getItem(position);

        final User user = data.getUser();

        //
        holder.mLlRoot.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                mActivity.setReply(data);
            }
        });

        //头像
        holder.mSdvAvatar.loadAvatar(user.getAvatarUrl(), user.getLevel());
        holder.mSdvAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开用户详情
                OuerDispatcher.presentUserActivity(mContext, user.getUserId(), user.getNick());
            }
        });
        //昵称
        holder.mTvName.setText(user.getNick());
        //时间
        holder.mTvTime.setText(UtilOuer.getFormatDate(mContext, data.getCreateAt(), "yy-M-d"));
        //评论
        Comment toComment = data.getToComment();
        if(toComment == null || toComment.getUser() == null) {
            holder.mTvComment.setText(data.getContent());
        } else {
            holder.mTvComment.setText(mContext.getString(R.string.share_comment_content_reply_user,
                    toComment.getUser().getNick(), data.getContent()));
        }
        //分割线
        if(position + 1 == getCount()) {
            holder.mViewLine.setVisibility(View.GONE);
        } else {
            holder.mViewLine.setVisibility(View.VISIBLE);
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        @Bind(R.id.comment_id_root)
        LinearLayout mLlRoot;
        //头像
        @Bind(R.id.comment_id_avatar)
        AvatarSimpleDraweeView mSdvAvatar;
        //昵称
        @Bind(R.id.comment_id_name)
        TextView mTvName;
        //时间
        @Bind(R.id.comment_id_time)
        TextView mTvTime;
        //评论
        @Bind(R.id.comment_id_content)
        TextView mTvComment;
        //分割线
        @Bind(R.id.comment_id_line)
        View mViewLine;
    }
}
